//
//  MyCollectionTableViewCell.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/25.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyCollectionTableViewCell.h"
#import "XHStarRateView.h"
#import <CoreLocation/CoreLocation.h>

@interface MyCollectionTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * shopLabel;
@property (nonatomic, weak)UIImageView * goodsImageView;
@property (nonatomic, weak)UILabel * goodsLabel;
@property (nonatomic, weak)UILabel * distanceLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commitLabel;
@property (nonatomic, weak)UILabel * dateLabel;
@property (nonatomic, weak)UILabel * priceLabel;
@property (nonatomic, weak)UILabel * orignLabel;
@property (nonatomic, weak)UILabel * soldLabel;
@property (nonatomic, weak)UIView * smallView;
@property (nonatomic, weak)UIImageView * tuanImageView;

@property (nonatomic, strong)UIButton * topButton;
@property (nonatomic, strong)UIButton * bottomButton;

@property (nonatomic, copy)NSString * shop_id;

@end

@implementation MyCollectionTableViewCell


- (void)setEdit:(BOOL)edit{
    
    _edit = edit;
    if (edit) {
        
        self.topButton.userInteractionEnabled = NO;
        self.bottomButton.userInteractionEnabled = NO;
    }else{
        
        self.topButton.userInteractionEnabled = YES;
        self.bottomButton.userInteractionEnabled = YES;
    }
}

- (void)setModel:(MyCollectionListModel *)model{
    
    _model = model;
    
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.merchants_img];
    NSURL * url = [NSURL URLWithString:usrlStr];
    [self.iconImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"组12"]];
    self.shopLabel.text = model.merchants_name;
    NSArray *goodsArray = [model.merchants_goods_img componentsSeparatedByString:@","];
    NSString * img = goodsArray.firstObject;
    NSString * goodsImg = [NSString stringWithFormat:@"%@%@",BASEURLSTR,img];
    NSURL * goodUrl = [NSURL URLWithString:goodsImg];
    [self.goodsImageView sd_setImageWithURL:goodUrl];
    self.goodsLabel.text = model.merchants_goods_name;
    int count = [model.merchants_goods_comments_star_level intValue];
    if(count == 0){
        
        self.starView.width = 1;
    }else{
        
        self.starView.currentRating = count;
    }
    
    self.commitLabel.text = [NSString stringWithFormat:@"%@条评论",model.merchants_goods_comments_number];
//    self.dateLabel.text = model.merchants_goods_available_time;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.merchatns_goods_platform_discount_price];
//    self.orignLabel.text = [NSString stringWithFormat:@"原价 ￥%@",model.merchants_goods_price];
    NSString * orString = [NSString stringWithFormat:@"￥%@",model.merchatns_goods_platform_price];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:orString attributes:attribtDic];
    
    self.orignLabel.attributedText = attribtStr;
    
    double lat = [model.merchants_latitude doubleValue];
    double lon = [model.merchants_longitude doubleValue];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    double res = [self distanceBetweenOrderBy:lat :userLat :lon :userLon];
    
    if (res > 1000) {
       self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res / 1000];
    }else{
       self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
    }
    
    self.goods_id = [NSString stringWithFormat:@"%@",model.goods_id];
    self.shop_id = [NSString stringWithFormat:@"%@",model.merchants_goods_merchants_id];
    int merchants_goods_type = [model.merchants_goods_type intValue];
    if (merchants_goods_type == 1) {
        
        self.tuanImageView.hidden = YES;
        self.dateLabel.text = [NSString stringWithFormat:@"%@",model.merchants_goods_available_time];
    }else{
        
        self.tuanImageView.hidden = NO;
        NSDictionary * dict = [self dictionaryWithJsonString:model.merchants_goods_available_time];
        NSArray *keysArray = [dict allKeys];
        NSString * key = keysArray.firstObject;
        NSArray * value = dict[key];
        NSString *string = [value componentsJoinedByString:@","];
        NSString * weekday;
        int week = [key intValue];
        switch (week) {
            case 1:
                weekday = @"周一";
                break;
            case 2:
                weekday = @"周二";
                break;
            case 3:
                weekday = @"周三";
                break;
            case 4:
                weekday = @"周四";
                break;
            case 5:
                weekday = @"周五";
                break;
            case 6:
                weekday = @"周六";
                break;
            case 7:
                weekday = @"周日";
                break;
                
            default:
                break;
        }
        self.dateLabel.text = [NSString stringWithFormat:@"%@ %@",weekday,string];
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF5F5F5);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = NO;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, 10 * SCREENH6, 30 * SCREENH6, 30 * SCREENH6)];
    
    iconImageView.backgroundColor = [UIColor lightGrayColor];
    iconImageView.layer.cornerRadius = 15 * SCREENH6;
    iconImageView.userInteractionEnabled = NO;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * shopLabel = [UILabel new];
    
    shopLabel.text = @"店铺名字...";
    shopLabel.textColor = ZRBlackColor;
    shopLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    shopLabel.userInteractionEnabled = NO;
    shopLabel.textAlignment = NSTextAlignmentLeft;
    
    self.shopLabel = shopLabel;
    
    [self.backView addSubview:self.shopLabel];
    
    self.topButton = [UIButton new];
    
    [self.topButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.topButton];
    
    UIImageView * goodsImageView = [UIImageView new];
    
    goodsImageView.backgroundColor = [UIColor lightGrayColor];
    goodsImageView.userInteractionEnabled = NO;
    
    self.goodsImageView = goodsImageView;
    
    [self.backView addSubview:self.goodsImageView];
    
    
    UILabel * goodsLabel = [UILabel new];
    
    goodsLabel.textColor = ZRBlackColor;
    goodsLabel.text = @"商品名称";
    goodsLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    goodsLabel.userInteractionEnabled = NO;
    
    self.goodsLabel = goodsLabel;
    
    [self.backView addSubview:self.goodsLabel];
    
    
    UILabel * distanceLabel = [UILabel new];
    
    distanceLabel.textColor = [UIColor lightGrayColor];
    distanceLabel.text = @"79m";
    distanceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    distanceLabel.userInteractionEnabled = NO;
    
    self.distanceLabel = distanceLabel;
    
    [self.backView addSubview:self.distanceLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(102 * SCREENH6, 73 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self.backView addSubview:self.starView];
    
    
    UILabel * commitLabel = [UILabel new];
    
    commitLabel.textColor = [UIColor lightGrayColor];
    commitLabel.text = @"11评论";
    commitLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    commitLabel.userInteractionEnabled = NO;
    
    self.commitLabel = commitLabel;
    
    [self.backView addSubview:self.commitLabel];
    
    
    UILabel * dateLabel = [UILabel new];
    
    dateLabel.textColor = ZRBlackColor;
    dateLabel.text = @"9月11日 - 9月21日";
    dateLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    dateLabel.userInteractionEnabled = NO;
    
    self.dateLabel = dateLabel;
    
    [self.backView addSubview:self.dateLabel];
    
    
    UIView * smallView = [UIView new];
    
    smallView.backgroundColor = ColorWithRGB(0xFFE055);
    smallView.userInteractionEnabled = NO;
    
    self.smallView = smallView;
    
    [self.backView addSubview:self.smallView];
    
    UILabel * priceLabel = [UILabel new];
    
    priceLabel.textColor = ZRBlackColor;
    priceLabel.text = @"$21.4";
    priceLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    priceLabel.userInteractionEnabled = NO;
    
    self.priceLabel = priceLabel;
    
    [self.backView addSubview:self.priceLabel];
    
    UILabel * orignLabel = [UILabel new];
    
    orignLabel.textColor = ZRBlackColor;
    orignLabel.text = @"原价$40.2";
    orignLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    orignLabel.userInteractionEnabled = NO;
    
    self.orignLabel = orignLabel;
    
    [self.backView addSubview:self.orignLabel];
    
    
    UILabel * soldLabel = [UILabel new];
    
    soldLabel.textColor = [UIColor lightGrayColor];
    soldLabel.text = @"已售34";
    soldLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    soldLabel.userInteractionEnabled = NO;
    
    self.soldLabel = soldLabel;
    
    [self.backView addSubview:self.soldLabel];
    
    UIButton * selectedButton = [UIButton new];
    
    selectedButton.hidden = YES;
    [selectedButton setImage:[UIImage imageNamed:@"椭圆2"] forState:UIControlStateNormal];
    [selectedButton setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
    [selectedButton addTarget:self action:@selector(clickSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
    selectedButton.userInteractionEnabled = NO;
    
    self.selectButton = selectedButton;
    
    [self.backView addSubview:self.selectButton];
    
    
    UIImageView * tuanImageView = [UIImageView new];
    
    tuanImageView.image = [UIImage imageNamed:@"首页拼团"];
    tuanImageView.hidden = YES;
    
    self.tuanImageView = tuanImageView;
    
    [self.contentView addSubview:self.tuanImageView];
    
    self.bottomButton = [UIButton new];
    
    [self.bottomButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.bottomButton];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(150 * SCREENH6);
    }];
    
//    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(30 * SCREENH6);
//        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
//        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
//    }];
    
    
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
    }];
    
    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView);
        make.height.width.mas_equalTo(85 * SCREENH6);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.tuanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.goodsImageView);
        make.width.mas_equalTo(45 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.goodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsImageView.mas_trailing).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.tuanImageView.mas_leading).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.goodsImageView);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.goodsLabel);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
    }];
    
    [self.commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.starView);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.trailing.mas_equalTo(self.tuanImageView.mas_leading).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(12 * SCREENH6);
        make.height.mas_equalTo(10 * SCREENH6);
        make.width.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.orignLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.priceLabel.mas_trailing).offset(7 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.soldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.distanceLabel);
        make.bottom.mas_equalTo(self.goodsImageView);
    }];
    
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.backView);
        make.bottom.mas_equalTo(self.goodsImageView.mas_top);
    }];
    
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.backView);
        make.top.mas_equalTo(self.goodsImageView);
    }];
}

-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance;
    
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    if (self.editing == editing)
    {
        return;
    }
    
    [super setEditing:editing animated:animated];
    
    if (self.editing) {
        
        self.selectButton.hidden = NO;

        self.iconImageView.x = 35 * SCREENH6;
        self.starView.x = 127 * SCREENH6;
    }else{
        
        self.selectButton.hidden = YES;
        [self.iconImageView setX:10 * SCREENH6];
        [self.starView setX:102 * SCREENH6];
    }
}


- (void)clickSelectedButton:(UIButton *)button{
    
//    button.selected = !button.selected;
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (void)clickButton:(UIButton *)button{
    
    NSString * type;
    NSString * ID;
    if (button == self.topButton) {
     
        type = @"top";
        ID = self.shop_id;
        NSDictionary * dict = @{@"type":type,@"ID":ID};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickCollectionCell" object:nil userInfo:dict];
    }else{
        
        type = @"bottom";
        ID = self.goods_id;
        NSString *  merchants_goods_type = [NSString stringWithFormat:@"%@",self.model.merchants_goods_type ];
        NSDictionary * dict = @{@"type":type,@"ID":ID,@"merchants_goods_type":merchants_goods_type};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickCollectionCell" object:nil userInfo:dict];
    }
    
    
}

@end
